package com.zenith.user.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.Result;
import com.zenith.common.result.PageResult;
import com.zenith.user.po.domain.Role;
import com.zenith.user.po.dto.RoleDTO;
import com.zenith.user.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理
 *
 * @author makejava
 * @since 2025-08-15
 */
@Slf4j
@RestController
@RequestMapping("/api/user/role")
@Tag(name = "角色管理")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @SecurePostMapping(value = "/addOrUpdateRole", roles = {"ADMIN"}, permission = "role:write")
    @Operation(summary = "添加或修改角色")
    public Result<Void> saveOrUpdate(@RequestBody Role entity) {
        log.info("添加或修改角色: {}", entity);
        roleService.saveOrUpdate(entity);
        return Result.success();
    }

    @SecurePostMapping(value = "/deleteRoles", roles = {"ADMIN"}, permission = "role:delete")
    @Operation(summary = "批量删除角色")
    public Result<Void> deleteRoles(@RequestBody List<String> ids) {
        log.info("批量删除角色: {}", ids);
        roleService.removeByIds(ids);
        return Result.success();
    }

    @SecurePostMapping(value = "/getRoleById", roles = {"ADMIN", "USER"}, permission = "role:read")
    @Operation(summary = "根据ID查询角色")
    public Result<Role> getRoleById(@RequestParam String id) {
        log.info("根据ID查询角色: {}", id);
        return Result.success(roleService.getById(id));
    }

    @SecurePostMapping(value = "/getRoleList", roles = {"ADMIN"},permission = "role:read")
    @Operation(summary = "获取角色列表")
    public Result<List<Role>> getRoleList() {
        log.info("获取角色列表");
        return Result.success(roleService.list());
    }

    @SecurePostMapping(value = "/getRolePage", roles = {"ADMIN"}, permission = "role:read")
    @Operation(summary = "分页查询角色")
    public Result<PageResult<Role>> getRolePage(RoleDTO query) {
        log.info("分页查询角色: {}", query);
        return Result.success(roleService.getRolePage(query));
    }

    @SecurePostMapping(value = "/updateRole", roles = {"ADMIN"}, permission = "role:update")
    @Operation(summary = "更新角色")
    public Result<Void> updateRole(@RequestBody Role entity) {
        log.info("更新角色: {}", entity);
        roleService.updateById(entity);
        return Result.success();
    }
}
